﻿using HelixToolkit.Wpf;

using LxCFD;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace TimeSolve
{
    public class GraphItem : BindableBase, IUpdataDisplay
    {
        public GraphItem(GraphTypes graphType)
        {
            meshDisType = MeshDisTypes.solid;
            loadMeshInf_new();
            ICollectionView cv = CollectionViewSource.GetDefaultView(meshSelectors);
            cv.GroupDescriptions.Add(new PropertyGroupDescription("thread.ownerZone.name"));
            //cv.GroupDescriptions.Add(new PropertyGroupDescription("thread.type"));


            GraphDataBase gdb = MainViewModel.mainViewModel.graphDataBase;
            this.graphCenter = gdb.graphBound.center;
            this.graphRadius = gdb.graphBound.radiusBound;
            viewport3D = new Viewport3D();
            viewport3D.Camera = new OrthographicCamera()
            {
                Position = gdb.graphBound.center + new Vector3D(0, 0, gdb.graphBound.radiusBound * 2),
                LookDirection = new Vector3D(0, 0, -1),
                UpDirection = new Vector3D(0, 1, 0)
            };

            Brush[] brushes = GraphDataBase.getBrushList();
            material = new DiffuseMaterial[brushes.Length];
            for (int i2 = 0; i2 < brushes.Length; i2++)
            {
                material[i2] = new DiffuseMaterial(brushes[i2]);
            }

            this.graphType = graphType;
            if (graphType == GraphTypes.网格图)
            {
                light = new DirectionalLight(Color.FromRgb(160, 160, 160), new Vector3D(0, 0, -1));
            }
            else
            {
                light = new AmbientLight(Color.FromRgb(125, 125, 125));
                colorBar = new ColorBar();
                colorBar.setColor(GraphDataBase.getSteppedGradientBrushDefault(), brushes.Length);
            }
        }

        public string name { get => _name; set { _name = value; RaisePropertyChanged("name"); } }
        private string _name;
        public double arrowScale { get => _arrowScale; set { _arrowScale = value; RaisePropertyChanged("arrowScale"); } }
        private double _arrowScale = 0.05;
        public GraphTypes graphType { get => _graphType; set { _graphType = value; isSourceChanged = true; } }
        private GraphTypes _graphType;
        public MeshDisTypes meshDisType { get => _meshDisType; set { _meshDisType = value; isSourceChanged = true; } }
        private MeshDisTypes _meshDisType;
        public ScalarTypes valueType
        {
            get => _valueType;
            set
            {
                _valueType = value;
                isSourceChanged = true;
                RaisePropertyChanged("valueType");
                //switch (_valueType)
                //{
                //    case ValueTypes.温度:
                //        getPhi = (fb) => { return fb?.getT(); };
                //        break;
                //    case ValueTypes.压力:
                //        getPhi = (fb) => { return fb?.getP(); };
                //        break;
                //    case ValueTypes.压力修正:
                //        getPhi = (fb) => { return fb?.getPcorrect(); };
                //        break;
                //    case ValueTypes.U:
                //        getPhi = (fb) => { return fb?.getU(); };
                //        break;
                //    case ValueTypes.V:
                //        getPhi = (fb) => { return fb?.getV(); };
                //        break;
                //    case ValueTypes.W:
                //        getPhi = (fb) => { return fb?.getW(); };
                //        break;
                //    case ValueTypes.k:
                //        getPhi = (fb) => { return fb?.getk(); };
                //        break;
                //    case ValueTypes.w:
                //        getPhi = (fb) => { return fb?.getw(); };
                //        break;
                //    case ValueTypes.质量不平衡:
                //        getPhi = (fb) => { return fb?.getMassImbalance(); };
                //        break;
                //    case ValueTypes.壁面距离:
                //        getPhi = (fb) => fb?.getWallDistance();
                //        break;
                //}
            }
        }
        private ScalarTypes _valueType;
        //Func<ZoneFields, double[]> getPhi = (fb) =>
        //{
        //    return fb?.getT();
        //};
        public bool isAutoRange { get => _isAutoRange; set { _isAutoRange = value; isSourceChanged = true; RaisePropertyChanged("isAutoRange"); } }
        private bool _isAutoRange = true;
        public double minValue { get => _minValue; set { _minValue = value; isSourceChanged = true; RaisePropertyChanged("minValue"); } }
        private double _minValue;
        public double maxValue { get => _maxValue; set { _maxValue = value; isSourceChanged = true; RaisePropertyChanged("maxValue"); } }
        private double _maxValue;

        public bool isSourceChanged { get => _isSourceChanged; set { _isSourceChanged = value; RaisePropertyChanged("isGraphUpdated"); } }
        private bool _isSourceChanged = true;


        public ObservableCollection<MeshSelector> meshSelectors { get; set; } = new ObservableCollection<MeshSelector>();
        public Viewport3D viewport3D { get; set; }
        public Light light { get; set; }
        public DiffuseMaterial[] material { get; set; }
        public DiffuseMaterial blankMaterial { get; set; } = new DiffuseMaterial(Brushes.Gray);
        public Model3DGroup model3DGroup { get; set; }
        public Point3D graphCenter { get; set; }
        public double graphRadius { get; set; }
        public ColorBar colorBar { get; set; }

        public void updateViewport3D()
        {
            if (MainViewModel.mainViewModel.graphDataBase == null) return;
            GraphDataBase gdb = MainViewModel.mainViewModel.graphDataBase;
            MeshVM mesh = MainViewModel.mainViewModel.mesh;
            viewport3D.Children.Clear();
            switch (graphType)
            {
                case GraphTypes.网格图:
                    if (meshDisType == MeshDisTypes.solid || meshDisType == MeshDisTypes.solidwire)
                    {
                        viewport3D.Children.Add(
                            new ModelVisual3D()
                            {
                                Content = getModel3Ds_geo()
                            }
                            );
                    }
                    if (meshDisType == MeshDisTypes.wire || meshDisType == MeshDisTypes.solidwire)
                    {
                        foreach (MeshSelector ms in meshSelectors)
                        {
                            if (ms.isSelected)
                            {
                                FvZone ownzone = ms.thread.RootZone;
                                LinesVisual3D line = new LinesVisual3D();
                                Point3DCollection ps = new Point3DCollection();
                                for (int iii = ms.thread.ThreadIndex.FaceIDs.Start; iii <= ms.thread.ThreadIndex.FaceIDs.End; iii++)//遍历thread
                                {
                                    ps.Add(ownzone.Nodes[ownzone.Faces[iii].NodeIDs[ownzone.Faces[iii].NodeIDs.Length - 1]]);
                                    for (int ii2 = 0; ii2 < ownzone.Faces[iii].NodeIDs.Length; ii2++)//遍历一个face
                                    {
                                        ps.Add(ownzone.Nodes[ownzone.Faces[iii].NodeIDs[ii2]]);
                                        ps.Add(ownzone.Nodes[ownzone.Faces[iii].NodeIDs[ii2]]);
                                    }
                                    ps.RemoveAt(ps.Count - 1);
                                }
                                line.Points = ps;
                                line.Thickness = 1;
                                line.Color = Colors.Black;
                                viewport3D.Children.Add(line);
                            }
                        }
                    }

                    break;
                case GraphTypes.等值图:
                    updateGeometry();
                    viewport3D.Children.Add(new ModelVisual3D() { Content = model3DGroup, });

                    break;
                case GraphTypes.矢量图:
                    updateArrow();
                    break;
                default:
                    break;
            }
            viewport3D.Children.Add(new ModelVisual3D() { Content = light });
        }

        private Model3DGroup getModel3Ds_geo()
        {
            GraphDataBase gdb = MainViewModel.mainViewModel.graphDataBase;
            MeshVM mesh = MainViewModel.mainViewModel.mesh;

            Brush[] brushes = GraphDataBase.getBrushList();
            int seg = brushes.Length;

            Model3DGroup model3DGroup = new Model3DGroup();
            DiffuseMaterial[] material = new DiffuseMaterial[seg];
            for (int i2 = 0; i2 < seg; i2++)
            {
                material[i2] = new DiffuseMaterial(brushes[i2]);
            }
            Random ran = new Random();

            foreach (MeshSelector ms in meshSelectors)
            {
                if (ms.isSelected)
                {
                    FvFace[] facesCopy = ms.thread.RootZone.Faces;
                    MeshGeometry3D[] meshgeos = new MeshGeometry3D[seg * 2];//有正反面
                    for (int i2 = 0; i2 < seg; i2++)
                    {
                        meshgeos[i2] = new MeshGeometry3D();
                        meshgeos[i2].TriangleIndices = new Int32Collection();
                        meshgeos[i2 + seg] = new MeshGeometry3D();
                        meshgeos[i2 + seg].TriangleIndices = new Int32Collection();
                        meshgeos[i2 + seg].Positions = meshgeos[i2].Positions = ms.thread.RootZone.Nodes;
                    }
                    for (int iii = ms.thread.ThreadIndex.FaceIDs.Start; iii <= ms.thread.ThreadIndex.FaceIDs.End; iii++)//遍历thread
                    {
                        int phiIndex = ran.Next(0, seg);
                        for (int ii2 = 2; ii2 < facesCopy[iii].NodeIDs.Length; ii2++)//遍历一个face
                        {
                            meshgeos[phiIndex].TriangleIndices.Add(facesCopy[iii].NodeIDs[0]);
                            meshgeos[phiIndex].TriangleIndices.Add(facesCopy[iii].NodeIDs[ii2 - 1]);
                            meshgeos[phiIndex].TriangleIndices.Add(facesCopy[iii].NodeIDs[ii2]);
                            meshgeos[phiIndex + seg].TriangleIndices.Add(facesCopy[iii].NodeIDs[0]);
                            meshgeos[phiIndex + seg].TriangleIndices.Add(facesCopy[iii].NodeIDs[ii2]);
                            meshgeos[phiIndex + seg].TriangleIndices.Add(facesCopy[iii].NodeIDs[ii2 - 1]);
                        }
                    }
                    for (int i3 = 0; i3 < seg; i3++)
                    {
                        if (meshgeos[i3].TriangleIndices.Count == 0) continue;//有bug，没有triangle也会有图形，因此增加这句
                        model3DGroup.Children.Add(new GeometryModel3D() { Geometry = meshgeos[i3], Material = material[i3] });
                        model3DGroup.Children.Add(new GeometryModel3D() { Geometry = meshgeos[i3 + seg], Material = material[i3] });
                    }
                }
            }
            return model3DGroup;
        }
        private Model3DGroup getModel3Ds_block()
        {
            return null;
        }
        private void updateArrow()
        {
            viewport3D.Children.Clear();
            GraphDataBase gdb = MainViewModel.mainViewModel.graphDataBase;
            MeshVM mesh = MainViewModel.mainViewModel.mesh;
            minValue = double.MaxValue;
            maxValue = 0;
            foreach (MeshSelector ms in meshSelectors)
            {
                if (ms.isSelected)
                {
                    if (ms.thread.RootZone.CFDFields?.GetArray(ScalarTypes.Vel_U) == null) return;
                    double[] us = ms.thread.RootZone.CFDFields.GetArray(ScalarTypes.Vel_U);
                    double[] vs = ms.thread.RootZone.CFDFields.GetArray(ScalarTypes.Vel_V);
                    double[] ws = ms.thread.RootZone.CFDFields.GetArray(ScalarTypes.Vel_W);
                    FvFace[] facesCopy = ms.thread.RootZone.Faces;
                    for (int i3 = ms.thread.ThreadIndex.FaceIDs.Start; i3 <= ms.thread.ThreadIndex.FaceIDs.End; i3++)
                    {
                        int cellindex = facesCopy[i3].OSideCell;
                        Vector3D v = new Vector3D(us[cellindex], vs[cellindex], ws[cellindex]);// v12 / v12.Length;
                        if (v.Length > maxValue)
                        {
                            maxValue = v.Length;
                        }
                        else if (v.Length < minValue)
                        {
                            minValue = v.Length;
                        }
                        double scale = arrowScale * v.Length;
                        if (v.Length == 0) continue;
                        v.Normalize();
                        Matrix3D matrix3D;
                        double x2 = v.X * v.X;
                        double y2 = v.Y * v.Y;
                        double x2y2 = x2 + y2;
                        if (x2y2 == 0)
                        {
                            if (Vector3D.DotProduct(new Vector3D(0, 0, 1), v) >= 0)
                            {
                                matrix3D = new Matrix3D();
                            }
                            else
                            {
                                matrix3D = new Matrix3D(
                                    1, 0, 0, 0,
                                    0, -1, 0, 0,
                                    0, 0, -1, 0,
                                    0, 0, 0, 1);
                            }
                        }
                        else
                        {
                            double x2d = x2 / x2y2;
                            double y2d = y2 / x2y2;
                            double xyd = v.X * v.Y / x2y2;
                            matrix3D = new Matrix3D(
                                y2d + x2d * v.Z, -xyd * (1 - v.Z), -v.X, 0,
                                -xyd * (1 - v.Z), x2d + y2d * v.Z, -v.Y, 0,
                                v.X, v.Y, v.Z, 0,
                                0, 0, 0, 1);
                        }

                        matrix3D.ScalePrepend(new Vector3D(scale, scale, scale));
                        matrix3D.Translate(facesCopy[i3].Center.ToVector3D());
                        MatrixTransform3D matrixTransform3D = new MatrixTransform3D(matrix3D);

                        model3DGroup = new Model3DGroup();
                        model3DGroup.Children.Add(gdb.arrow);
                        model3DGroup.Transform = matrixTransform3D;
                        viewport3D.Children.Add(new ModelVisual3D() { Content = model3DGroup });
                    }
                }
            }
            colorBar.setColorRange(minValue, maxValue);
        }
        private void updateGeometry()
        {
            GraphDataBase gdb = MainViewModel.mainViewModel.graphDataBase;
            MeshVM mesh = MainViewModel.mainViewModel.mesh;
            model3DGroup = new Model3DGroup();
            foreach (MeshSelector ms in meshSelectors)
            {
                if (ms.isSelected)
                {
                    FvFace[] facesCopy = ms.thread.RootZone.Faces;
                    for (int i3 = ms.thread.ThreadIndex.FaceIDs.Start; i3 <= ms.thread.ThreadIndex.FaceIDs.End; i3++)
                    {
                        Int32Collection zhengmian = new Int32Collection();
                        Int32Collection fanmian = new Int32Collection();
                        for (int i4 = 2; i4 < facesCopy[i3].NodeIDs.Length; i4++)
                        {
                            zhengmian.Add(facesCopy[i3].NodeIDs[0]);
                            zhengmian.Add(facesCopy[i3].NodeIDs[i4 - 1]);
                            zhengmian.Add(facesCopy[i3].NodeIDs[i4]);
                            fanmian.Add(facesCopy[i3].NodeIDs[0]);
                            fanmian.Add(facesCopy[i3].NodeIDs[i4]);
                            fanmian.Add(facesCopy[i3].NodeIDs[i4 - 1]);
                        }
                        GeometryModel3D zheng = new GeometryModel3D()
                        {
                            Geometry = new MeshGeometry3D()
                            {
                                Positions = ms.thread.RootZone.Nodes,
                                TriangleIndices = zhengmian,
                            },
                        };
                        GeometryModel3D fan = new GeometryModel3D()
                        {
                            Geometry = new MeshGeometry3D()
                            {
                                Positions = ms.thread.RootZone.Nodes,
                                TriangleIndices = fanmian,
                            },
                        };
                        model3DGroup.Children.Add(zheng);
                        model3DGroup.Children.Add(fan);
                    }
                }
            }
            updateMaterial();
        }
        private void updateMaterial()
        {
            //Func<FieldsBase, double[]> getPhi = (fb) =>
            //{
            //    return fb?.getT();
            //};
            foreach (MeshSelector ms in meshSelectors)
            {
                if (ms.isSelected)
                {
                    double[] phi = ms.thread.RootZone.CFDFields.GetArray(valueType);// getPhi(ms.thread.ownerZone.fields);                    
                    if (phi == null) continue;
                    FvFace[] faces = ms.thread.RootZone.Faces;
                    minValue = maxValue = phi[faces[ms.thread.ThreadIndex.FaceIDs.Start].NSideCell];
                    break;
                }
            }
            foreach (MeshSelector ms in meshSelectors)
            {
                if (ms.isSelected)
                {
                    double[] phi = ms.thread.RootZone.CFDFields.GetArray(valueType); // getPhi(ms.thread.ownerZone.fields);
                    if (phi == null) continue;
                    FvFace[] faces = ms.thread.RootZone.Faces;
                    for (int i1 = ms.thread.ThreadIndex.FaceIDs.Start; i1 <= ms.thread.ThreadIndex.FaceIDs.End; i1++)
                    {
                        if (phi[faces[i1].NSideCell] > maxValue)
                        {
                            maxValue = phi[faces[i1].NSideCell];
                        }
                        if (phi[faces[i1].NSideCell] < minValue)
                        {
                            minValue = phi[faces[i1].NSideCell];
                        }
                    }
                }
            }
            double deltaMaxMin = maxValue - minValue;
            if (deltaMaxMin == 0) deltaMaxMin = 1;
            int seg = material.Length;
            colorBar.setColorRange(minValue, maxValue);
            Func<double, int> calPhiIndex = (phi) =>
            {
                int phiIndex = (int)((phi - minValue) / deltaMaxMin * seg);
                if (phiIndex == seg) phiIndex--;
                return phiIndex;
            };

            int nlog = 0;
            foreach (MeshSelector ms in meshSelectors)
            {
                if (ms.isSelected)
                {
                    FvFace[] facesCopy = ms.thread.RootZone.Faces;
                    double[] phi = ms.thread.RootZone.CFDFields.GetArray(valueType);// getPhi(ms.thread.ownerZone.fields);
                    if (phi == null)
                    {
                        for (int i3 = ms.thread.ThreadIndex.FaceIDs.Start; i3 <= ms.thread.ThreadIndex.FaceIDs.End; i3++)
                        {
                            ((GeometryModel3D)model3DGroup.Children[nlog]).Material = blankMaterial;
                            nlog++;
                            ((GeometryModel3D)model3DGroup.Children[nlog]).Material = blankMaterial;
                            nlog++;
                        }
                    }
                    else
                    {
                        for (int i3 = ms.thread.ThreadIndex.FaceIDs.Start; i3 <= ms.thread.ThreadIndex.FaceIDs.End; i3++)
                        {
                            //
                            int phiIndex = calPhiIndex(phi[facesCopy[i3].NSideCell]);//NeighborCellIndex//这个地方切换的，后面记得改回来
                            ((GeometryModel3D)model3DGroup.Children[nlog]).Material = material[phiIndex];
                            nlog++;
                            ((GeometryModel3D)model3DGroup.Children[nlog]).Material = material[phiIndex];
                            nlog++;
                        }
                    }

                }
            }
        }

        public void loadMeshInf_new()
        {
            if (MainViewModel.mainViewModel.mesh == null) return;
            foreach (FvZone z in AppRes.FvMesh.Zones)
            {
                foreach (FvThread t in z.BoundThreads)
                {
                    meshSelectors.Add(new MeshSelector(this) { thread = t, isSelected = false });
                }
            }

        }
        //更新包括图形
        public void updateFully()
        {
            if (true)
            {
                updateViewport3D();
            }
        }
        //更新云图颜色
        public void updatePartly()
        {
            if (graphType == GraphTypes.网格图) return;
            if (graphType == GraphTypes.矢量图)
            {
                updateViewport3D();
                return;
            }
            updateMaterial();
        }


        //public void loadMeshInf_add(Mesh.MeshProfile m)
        //{
        //   // meshSelectors.Add(new MeshSelector(this) { meshProfile = m, isSelected = false });
        //}
        public class MeshSelector
        {
            public MeshSelector(GraphItem hostGraphItem)
            {
                this.hostGraphItem = hostGraphItem;
            }
            private GraphItem hostGraphItem;
            public FvThread thread { get; set; }
            public bool isSelected { get => _isSelected; set { _isSelected = value; hostGraphItem.isSourceChanged = false; } }
            private bool _isSelected = false;
        }
        public struct BrushCalculator
        {
            public double value;
            public double xi;
            public void addValue(double newValue, double newXi)
            {
                value += newValue * newXi;
                xi += newXi;
            }
            public double calAverage()
            {
                return value / xi;
            }
        }
    }
    public enum MeshDisTypes
    {
        solid,
        wire,
        solidwire
    }
    public enum GraphTypes
    {
        网格图,
        等值图,
        矢量图,
        迹线图
    }
}




//private void addVisual3D(Viewport3D targetViewport3D,Func<ModelBase, double[]> getPhi)
//{
//    minValue = double.MaxValue;
//    maxValue = double.MinValue;

//    foreach (MeshSelector ms in meshSelectors)
//    {
//        if (ms.isSelected)
//        {
//            double[] phi = getPhi(ms.thread.ownerZone.modelValue);
//            Mesh.Face[] faces = ms.thread.ownerZone.faces;
//            for (int i1 = ms.thread.first_index; i1 <= ms.thread.last_index; i1++)
//            {
//                if (phi[faces[i1].cell1] > maxValue)
//                {
//                    maxValue = phi[faces[i1].cell1];
//                }
//                if (phi[faces[i1].cell1] < minValue)
//                {
//                    minValue = phi[faces[i1].cell1];
//                }
//            }
//        }
//    }
//    double deltaMaxMin = maxValue - minValue;
//    if (deltaMaxMin == 0) deltaMaxMin = maxValue;
//    Brush brush = GraphDataBase.getDefaultGradientBrush();
//    GraphDataBase gdb = MainViewModel.mainViewModel.graphDataBase;
//    Mesh mesh = MainViewModel.mainViewModel.mesh;

//    int[] tempPointIndex = new int[mesh.nodes.Count];
//    foreach (MeshSelector ms in meshSelectors)
//    {
//        if (ms.isSelected)
//        {
//            double[] phi = getPhi(ms.thread.ownerZone.modelValue);
//            for (int i1 = 0; i1 < tempPointIndex.Length; i1++)
//            {
//                tempPointIndex[i1] = -1;
//            }
//            Point3DCollection ps = new Point3DCollection();
//            Int32Collection ic0 = new Int32Collection();
//            Int32Collection ic1 = new Int32Collection();
//            Mesh.Face[] facesCopy = ms.thread.ownerZone.faces;
//            MeshData meshDataCopy = ms.thread.ownerZone.meshData;
//            List<BrushCalculator> brushCal = new List<BrushCalculator>();
//            for (int iii = ms.thread.first_index; iii <= ms.thread.last_index; iii++)//遍历thread
//            {
//                for (int ii1 = 0; ii1 < facesCopy[iii].facenodes.Length; ii1++)
//                {
//                    if (tempPointIndex[facesCopy[iii].facenodes[ii1]] < 0)
//                    {
//                        tempPointIndex[facesCopy[iii].facenodes[ii1]] = ps.Count;
//                        ps.Add(mesh.nodes[facesCopy[iii].facenodes[ii1]]);
//                        BrushCalculator brc = new BrushCalculator();
//                        brc.addValue(phi[facesCopy[iii].cell1], 1 / (mesh.nodes[facesCopy[iii].facenodes[ii1]] - meshDataCopy.faceDatas[iii].Center).Length);
//                        brushCal.Add(brc);
//                    }else
//                    {
//                        brushCal[tempPointIndex[facesCopy[iii].facenodes[ii1]]].
//                            addValue(phi[facesCopy[iii].cell1], 1 / (mesh.nodes[facesCopy[iii].facenodes[ii1]] - meshDataCopy.faceDatas[iii].Center).Length);
//                    }
//                }
//                for (int ii2 = 2; ii2 < ms.thread.ownerZone.faces[iii].facenodes.Length; ii2++)//遍历一个face
//                {
//                    ic0.Add(tempPointIndex[facesCopy[iii].facenodes[0]]);
//                    ic0.Add(tempPointIndex[facesCopy[iii].facenodes[ii2 - 1]]);
//                    ic0.Add(tempPointIndex[facesCopy[iii].facenodes[ii2]]);
//                    ic1.Add(tempPointIndex[facesCopy[iii].facenodes[0]]);
//                    ic1.Add(tempPointIndex[facesCopy[iii].facenodes[ii2]]);
//                    ic1.Add(tempPointIndex[facesCopy[iii].facenodes[ii2 - 1]]);
//                }
//            }
//            PointCollection texture = new PointCollection();
//            foreach (BrushCalculator bc in brushCal)
//            {
//                texture.Add(new System.Windows.Point(0, (bc.calAverage() - minValue)/ deltaMaxMin));
//            }

//            MeshGeometryVisual3D hv = new MeshGeometryVisual3D();
//            hv.Fill = brush;
//            MeshGeometry3D mgeo = new MeshGeometry3D();
//            mgeo.Positions = ps;
//            mgeo.TriangleIndices = ic0;
//            mgeo.TextureCoordinates = texture;
//            hv.MeshGeometry = mgeo;
//            targetViewport3D.Children.Add(hv);

//            MeshGeometryVisual3D hv2 = new MeshGeometryVisual3D();
//            hv2.Fill = brush;
//            MeshGeometry3D mgeo2 = new MeshGeometry3D();
//            mgeo2.Positions = ps;
//            mgeo2.TriangleIndices = ic1;
//            mgeo2.TextureCoordinates = texture;
//            hv2.MeshGeometry = mgeo2;
//            targetViewport3D.Children.Add(hv2);
//        }
//    }

//}


//        switch (meshDisType)
//                    {
//                        case MeshDisTypes.solid:
//                            //foreach (MeshSelector ms in meshSelectors)
//                            //{
//                            //    if (ms.isSelected)
//                            //    {
//                            //        GeometryModel3D g;
//                            //        LinesVisual3D l;

//                            //         MainViewModel.mainViewModel.buildGraph.buildMeshWithWire(out g,out l,MainViewModel.mainViewModel.mesh.faces, ms.meshProfile.first_index, ms.meshProfile.last_index, MeshRenderLib.getBrush(random.Next(0, 6)));
//                            //        this.graphViewport.model3DGroup.Children.Add(g);
//                            //        this.graphViewport.lines = l;
//                            //    }
//                            //}


//                            break;
//                        case MeshDisTypes.wire:
//                            //graphViewport.lines = MainViewModel .mainViewModel .buildGraph.buildMeshWire();

//                            //foreach (MeshSelector ms in meshSelectors)
//                            //{
//                            //    if (ms.isSelected)
//                            //    {
//                            //        LinesVisual3D linesVisual3D = new LinesVisual3D();
//                            //        linesVisual3D.Color = Colors.Black;
//                            //        linesVisual3D.Thickness = 1;

//                            //    }
//                            //}

//                            graphViewport.setViewport3D();
//                            MainViewModel.mainViewModel.showControlCurrent.loadGraph(this.graphViewport);
//                            break;
//                        case MeshDisTypes.solidwire:

//                            break;
//                        default:
//                            break;
//}

//Random random = new Random();
//Brush brush = GraphDataBase.getDefaultGradientBrush();
//int[] tempPointIndex = new int[mesh.nodes.Count];
//foreach (MeshSelector ms in meshSelectors)
//{
//    if (ms.isSelected)
//    {
//        for(int i1 = 0; i1 < tempPointIndex.Length; i1++)
//        {
//            tempPointIndex[i1] = -1;
//        }
//        Point3DCollection ps = new Point3DCollection();
//        Int32Collection ic0 = new Int32Collection();
//        Int32Collection ic1 = new Int32Collection();
//        Mesh.Face[] facesTemp = ms.thread.ownerZone.faces;
//        for (int iii = ms.thread.first_index; iii <= ms.thread.last_index; iii++)//遍历thread
//        {    
//            for(int ii1 = 0; ii1 < facesTemp[iii].facenodes.Length; ii1++)
//            {
//                if (tempPointIndex[facesTemp[iii].facenodes[ii1]] < 0)
//                {
//                    tempPointIndex[facesTemp[iii].facenodes[ii1]] = ps.Count;
//                    ps.Add(mesh.nodes[facesTemp[iii].facenodes[ii1]]);
//                }
//            }
//            for (int ii2 = 2; ii2 < ms.thread.ownerZone.faces[iii].facenodes.Length; ii2++)//遍历一个face
//            {
//                ic0.Add(tempPointIndex[ facesTemp[iii].facenodes[0]]);
//                ic0.Add(tempPointIndex[ facesTemp[iii].facenodes[ii2 - 1]]);
//                ic0.Add(tempPointIndex[facesTemp[iii].facenodes[ii2]]);
//                ic1.Add(tempPointIndex[facesTemp[iii].facenodes[0]]);
//                ic1.Add(tempPointIndex[facesTemp[iii].facenodes[ii2]]);
//                ic1.Add(tempPointIndex[facesTemp[iii].facenodes[ii2 - 1]]);
//            }
//        }
//        //Brush br = GraphDataBase.getBrush(random.Next(0, 5));
//        PointCollection texture = new PointCollection();
//        double x = random.NextDouble();
//        for(int iii=0; iii <ps.Count; iii++)
//        {
//            texture.Add(new System.Windows.Point(x,x));
//        }
//        MeshGeometryVisual3D hv = new MeshGeometryVisual3D();
//        hv.Fill = brush;
//        MeshGeometry3D mgeo = new MeshGeometry3D();
//        mgeo.Positions = ps;
//        mgeo.TriangleIndices = ic0;
//        mgeo.TextureCoordinates = texture;
//        hv.MeshGeometry = mgeo;
//        viewport3D.Children.Add(hv);

//        MeshGeometryVisual3D hv2 = new MeshGeometryVisual3D();
//        hv2.Fill = brush;
//        MeshGeometry3D mgeo2 = new MeshGeometry3D();
//        mgeo2.Positions = ps;
//        mgeo2.TriangleIndices = ic1;
//        mgeo2.TextureCoordinates = texture;
//        hv2.MeshGeometry = mgeo2;
//        viewport3D.Children.Add(hv2);
//    }
//}


//public Viewport3D getViewport3D()
//{
//    if (MainViewModel.mainViewModel.graphDataBase == null) return null;
//    GraphDataBase gdb = MainViewModel.mainViewModel.graphDataBase;
//    Mesh mesh = MainViewModel.mainViewModel.mesh;
//    this.graphCenter = gdb.graphBound.center;
//    this.graphRadius = gdb.graphBound.radiusBound;

//    Viewport3D viewport3D = new Viewport3D();
//    if(camera == null)
//    {
//        camera = new OrthographicCamera()
//        {
//            Position = gdb.graphBound.center + new Vector3D(0, 0, gdb.graphBound.radiusBound * 2),
//            LookDirection = new Vector3D(0, 0, -1),
//            UpDirection = new Vector3D(0, 1, 0)
//        };
//    }
//    viewport3D.Camera = this.camera;
//    switch (graphType)
//    {
//        case GraphTypes.网格图:
//            if (meshDisType == MeshDisTypes.solid || meshDisType == MeshDisTypes.solidwire)
//            {

//                viewport3D.Children.Add(
//                    new ModelVisual3D()
//                    {
//                        Content = getModel3Ds_geo()
//                    }
//                    );
//            }
//            if (meshDisType == MeshDisTypes.wire || meshDisType == MeshDisTypes.solidwire)
//            {
//                foreach (MeshSelector ms in meshSelectors)
//                {
//                    if (ms.isSelected)
//                    {
//                        Mesh.Zone ownzone = ms.thread.ownerZone;
//                        LinesVisual3D line = new LinesVisual3D();
//                        Point3DCollection ps = new Point3DCollection();
//                        for (int iii = ms.thread.first_index; iii <= ms.thread.last_index; iii++)//遍历thread
//                        {
//                            ps.Add(ownzone.nodes[ownzone.faces[iii].nodesIndex[ownzone.faces[iii].nodesIndex.Length - 1]]);
//                            for (int ii2 = 0; ii2 < ownzone.faces[iii].nodesIndex.Length; ii2++)//遍历一个face
//                            {
//                                ps.Add(ownzone.nodes[ownzone.faces[iii].nodesIndex[ii2]]);
//                                ps.Add(ownzone.nodes[ownzone.faces[iii].nodesIndex[ii2]]);
//                            }
//                            ps.RemoveAt(ps.Count - 1);
//                        }
//                        line.Points = ps;
//                        line.Thickness = 1;
//                        line.Color = Colors.Black;
//                        viewport3D.Children.Add(line);
//                    }
//                }
//            }
//            break;
//        case GraphTypes.等值图:
//            switch (valueType)
//            {
//                case ValueTypes.温度:
//                    viewport3D.Children.Add(
//                        new ModelVisual3D()
//                        {
//                            Content = getModel3Ds()
//                        }
//                        );
//                    break;
//                default:
//                    break;
//            }
//            break;
//        default:
//            break;
//    }
//    return viewport3D;
//}